# Template file for 'cuda'
pkgname=cuda
version=12.2.0
revision=1
_driverver=535.54.03
archs="x86_64"
create_wrksrc=yes
hostmakedepends="tar"
depends="nvidia-opencl nvidia-libs glu gdb"
short_desc="NVIDIA's GPU programming toolkit"
maintainer="MarcoAPC <marcoaureliopc@gmail.com>"
license="custom:NVIDIA Proprietary"
homepage="https://developer.nvidia.com/cuda-zone"
distfiles="https://developer.download.nvidia.com/compute/cuda/${version}/local_installers/cuda_${version}_${_driverver}_linux.run"
checksum=ecf3d2afadcbac029f0f4505785810d52d006e4b87ba79ff3f984336a2bbf518
repository="nonfree"
nopie=yes
subpackages="cuda-tools"

do_extract() {
	sh ${XBPS_SRCDISTDIR}/${pkgname}-${version}/cuda_${version}_${_driverver}_linux.run --target ${wrksrc} --noexec
	cd ${wrksrc}
}

do_install() {
	local _prepdir="${DESTDIR}"

	cd "${wrksrc}/builds"

	rm -r NVIDIA*.run bin
	mkdir -p "${_prepdir}/opt/cuda/extras"
	mv integration nsight_compute nsight_systems EULA.txt "${_prepdir}/opt/cuda"
	mv cuda_demo_suite/extras/demo_suite "${_prepdir}/opt/cuda/extras/demo_suite"
	mv cuda_sanitizer_api/compute-sanitizer "${_prepdir}/opt/cuda/extras/compute-sanitizer"
	rmdir cuda_sanitizer_api
	for lib in *; do
		if [[ "$lib" =~ .*"version.json".* ]]; then
		continue
		fi
		cp -r $lib/* "${_prepdir}/opt/cuda/"
	done

	# Delete some unnecessary files
	rm -r "${_prepdir}"/opt/cuda/bin/cuda-uninstaller

	# Define compilers for CUDA to use.
	# This allows us to use older versions of GCC if we have to.
	ln -s /usr/bin/gcc "${_prepdir}/opt/cuda/bin/gcc"
	ln -s /usr/bin/g++ "${_prepdir}/opt/cuda/bin/g++"

	# Install profile and ld.so.config files
	vinstall "${FILESDIR}/cuda.sh" 755 "etc/profile.d/cuda.sh"
	vinstall "${FILESDIR}/cuda.conf" 644 "etc/ld.so.conf.d/cuda.conf"

	# Install pkgconfig files
	mkdir -p "$_prepdir"/usr/lib/pkgconfig
	cp "${FILESDIR}"/*.pc "${_prepdir}"/usr/lib/pkgconfig

	mkdir -p "${_prepdir}/usr/share/licenses/${pkgname}"
	ln -s /opt/cuda/EULA.txt "${_prepdir}/usr/share/licenses/${pkgname}/EULA.txt"
	ln -s /opt/cuda/README "${_prepdir}/usr/share/licenses/${pkgname}/README"

	# Allow newer compilers to work.  
	# See https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#system-requirements
	# for official requirements
	sed -i "/.*unsupported GNU version.*/d" "${_prepdir}"/opt/cuda/targets/x86_64-linux/include/crt/host_config.h
	sed -i "/.*unsupported clang version.*/d" "${_prepdir}"/opt/cuda/targets/x86_64-linux/include/crt/host_config.h

	# Fix Makefile paths to CUDA
	for f in $(find "$_prepdir"/opt/cuda -name Makefile); do
		sed -i "s|/usr/local/cuda|/opt/cuda|g" "$f"
	done
}

cuda-tools_package() {
	short_desc="${_desc} - (extra tools: nvvp, nsight)"
	depends="cuda openjdk8-jre nss perl ucx"
	python_version=3
	nostrip_files="TreeLauncherTargetLdPreloadHelper TreeLauncherSubreaper ncu libcuda-injection.so libInterceptorInjectionTarget.so libnvperf_host.so libnvperf_target.so libTreeLauncherPlaceholder.so libTreeLauncherTargetInjection.so libTreeLauncherTargetUpdatePreloadInjection.so"
	pkg_install() {
		vmove opt/cuda/nsight*
		vmove opt/cuda/bin/nvvp
		vmove opt/cuda/bin/computeprof
		vmove opt/cuda/libnvvp
	}
}
